Code Challenge Season 2

add up Positive and Negative (Lv. 1)
https://school.programmers.co.kr/learn/courses/30/lessons/76501
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
int solution(vector<int> absolutes, vector<bool> signs){
int answer=0;
assert(absolutes.size()==signs.size());
for(int i=0; i<absolutes.size(); ++i){
answer+=signs[i]? absolutes[i]: -absolutes[i];
}
return answer;
}
int main(void){
vector<int> absolutes1={4, 7, 12};
vector<int> absolutes2={1, 2, 3};
vector<bool> signs1={true, false, true};
vector<bool> signs2={false, false, true};
int result1, result2;
result1=solution(absolutes1, signs1);
result2=solution(absolutes2, signs2);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
return 0;
}
Count and sum of Common measure (Lv. 1)
https://school.programmers.co.kr/learn/courses/30/lessons/77884
#include <iostream>
#include <vector>
using namespace std;
int solution(int left, int right){
int answer=0;
for(int num=left; num<right+1; ++num){
vector<int> storage;
for(int i=1; i<num+1; ++i){
if(num%i==0) storage.push_back(i);
}
if(storage.size()%2) answer-=num;
else answer+=num;
}
return answer;
}
int main(void){
int left1=13, left2=24, right1=17, right2=27, result1, result2;
result1=solution(left1, right1);
result2=solution(left2, right2);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
return 0;
}
Rotate Bracket (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/76502
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
int check(const string s){
map<char, int> m={
{'[', 0}, {'(', 0}, {'{', 0}
};
for(int i=0; i<s.length(); ++i){
if(s[i]=='['){
m['[']++;
continue;
}
if(s[i]=='('){
m['(']++;
continue;
}
if(s[i]=='{'){
m['{']++;
continue;
}
if(s[i]==']' && m['[']!=0){
m['[']--;
}else if(s[i]==')' && m['(']!=0){
m['(']--;
}else if(s[i]=='}' && m['{']!=0){
m['{']--;
} else return 0;
}
return 1;
}
int solution(const string s){
int answer=0;
for(int i=0; i<s.length(); ++i){
string str=s.substr(i, s.length()-i)+s.substr(0, i);
answer+=check(str);
}
return answer;
}
int main(void){
string s1="[](){}";
string s2="}]()[{";
string s3="[)(]";
string s4="}}}";
int result1, result2, result3, result4;
result1=solution(s1);
result2=solution(s2);
result3=solution(s3);
result4=solution(s4);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
cout<<"result3: "<<result3<<endl;
cout<<"result4: "<<result4<<endl;
return 0;
}
less than two different bits (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/77885
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;
vector<int> ten_two(int num){
vector<int> bit;
while(num){
bit.push_back(num%2);
num/=2;
}
reverse(bit.begin(), bit.end());
return bit;
}
bool count_diff(vector<int> bit1, vector<int> bit2){
int sum=0, min_size, max_size;
min_size=min(bit1.size(), bit2.size());
max_size=max(bit1.size(), bit2.size());
for(int i=0; i<=min_size; ++i){
if(bit1[bit1.size()-i]!=bit2[bit2.size()-i]) sum++;
}
sum+=max_size-min_size;
return sum<=2;
}
vector<ll> solution(vector<ll> numbers){
vector<ll> answer;
for(int i=0; i<numbers.size(); ++i){
vector<int> bit=ten_two(numbers[i]);
for(int j=numbers[i]+1; ; ++j){
vector<int> big=ten_two(j);
if(count_diff(bit, big)){
answer.push_back(j);
break;
}
}
}
return answer;
}
int main(void){
vector<ll> numbers={2, 7};
vector<ll> result=solution(numbers);
cout<<"{"<<result[0]<<", "<<result[1]<<"}"<<endl;
return 0;
}
move 110 (Lv. 3)
https://school.programmers.co.kr/learn/courses/30/lessons/77886
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<string> solution(const vector<string> s){
vector<string> answer;
for(string str: s){
string tmp_str=str;
int point=0;
while(1){
vector<string> strset;
string prev_str=tmp_str;
if(tmp_str.find("110", point)!=string::npos){
point=tmp_str.find("110", point);
string tmp=tmp_str.erase(point, 3);
for(int i=0; i<=tmp.length(); ++i){
string ttmp=tmp;
strset.push_back(ttmp.insert(i, "110"));
}
sort(strset.begin(), strset.end());
tmp_str=strset[0];
if(point>str.length()){
answer.push_back(tmp_str);
break;
}
if(tmp_str==prev_str){
prev_str=tmp_str;
point+=3;
} else {
point=0;
}
} else {
answer.push_back(tmp_str);
break;
}
}
}
return answer;
}
int main(void){
vector<string> s={
"1110", "100111100", "0111111010"
};
vector<string> result=solution(s);
if(result.size()){
cout<<"[";
for(int i=0; i<result.size()-1; ++i) cout<<result[i]<<", ";
cout<<result[result.size()-1]<<"]"<<endl;
}
return 0;
}
Make Zeros (Lv. 3)
https://school.programmers.co.kr/learn/courses/30/lessons/76503
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
struct _dist{
int distance=0;
vector<int> pre_root;
bool check(int point){
return find(pre_root.begin(), pre_root.end(), point)==pre_root.end();
}
};
void find_route(vector<vector<int>>& edges, int point1, const int point2, _dist dd, int& dist){
if(point1==point2){
dist=dd.distance<dist? dd.distance: dist;
return;
}
for(int i=0; i<edges.size(); ++i){
if(edges[i][0]==point1 && dd.check(edges[i][1])){
_dist tmp=dd;
tmp.distance++;
tmp.pre_root.push_back(point1);
find_route(edges, edges[i][1], point2, tmp, dist);
} else if (edges[i][1]==point1 && dd.check(edges[i][0])){
_dist tmp=dd;
tmp.distance++;
tmp.pre_root.push_back(point1);
find_route(edges, edges[i][0], point2, tmp, dist);
}
}
}
ll solution(vector<int> a, vector<vector<int>> edges){
ll answer=0, zeros_count=0, sum;
for(int aa: a){
if(aa) zeros_count++;
answer+=aa;
}
if(zeros_count==a.size()) return 0;
if(answer) return -1;
for(int i=0; i<a.size(); ++i){
sum=0;
for(int j=0; j<a.size(); ++j){
if(i==j) continue;
int dist=a.size();
_dist dd;
find_route(edges, j, i, dd, dist);
sum+=dist*abs(a[j]);
}
if(!i) answer=sum;
else answer=answer>sum? sum: answer;
}
return answer;
}
int main(void){
vector<int> a1={-5, 0, 2, 1, 2};
vector<int> a2={0, 1, 0};
vector<vector<int>> edges1={
{0, 1}, {3, 4}, {2, 3}, {0, 3}
};
vector<vector<int>> edges2={
{0, 1}, {1, 2}
};
int result1=solution(a1, edges1);
int result2=solution(a2, edges2);
cout<<"Result1: "<<result1<<endl;
cout<<"Result2: "<<result2<<endl;
return 0;
}